package com.dankegongyu.lib.common.widget.recyclerview;

import android.graphics.Rect;
import androidx.annotation.NonNull;
import androidx.recyclerview.widget.GridLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import android.view.View;

public class LoadMoreGridSpaceItemDecoration extends RecyclerView.ItemDecoration {

    private LoadMoreRecyclerView mRecyclerView;
    private int spanCount;
    private int horizontalSpace;
    private int verticalSpace;
    private boolean includeEdge;

    /**
     * @param loadMoreRecyclerView {@link LoadMoreRecyclerView}
     * @param space                分割线宽度
     */
    public LoadMoreGridSpaceItemDecoration(LoadMoreRecyclerView loadMoreRecyclerView, int space) {
        this(loadMoreRecyclerView, space, false);
    }

    /**
     * @param loadMoreRecyclerView {@link LoadMoreRecyclerView}
     * @param space                分割线宽度
     * @param includeEdge          屏幕所有两边是否需要画分割线
     */
    public LoadMoreGridSpaceItemDecoration(LoadMoreRecyclerView loadMoreRecyclerView, int space, boolean includeEdge) {
        this(loadMoreRecyclerView, space, space, includeEdge);
    }

    /**
     * @param loadMoreRecyclerView {@link LoadMoreRecyclerView}
     * @param horizontalSpace      横间距
     * @param verticalSpace        纵间距
     */
    public LoadMoreGridSpaceItemDecoration(LoadMoreRecyclerView loadMoreRecyclerView, int horizontalSpace, int verticalSpace) {
        this(loadMoreRecyclerView, horizontalSpace, verticalSpace, false);
    }

    /**
     * @param loadMoreRecyclerView {@link LoadMoreRecyclerView}
     * @param horizontalSpace      横间距
     * @param verticalSpace        纵间距
     * @param includeEdge          屏幕所有两边是否需要画分割线
     */
    public LoadMoreGridSpaceItemDecoration(LoadMoreRecyclerView loadMoreRecyclerView, int horizontalSpace, int verticalSpace, boolean includeEdge) {
        this.mRecyclerView = loadMoreRecyclerView;
        if (mRecyclerView == null) {
            return;
        }
        RecyclerView.LayoutManager layoutManager = mRecyclerView.getLayoutManager();
        if (layoutManager instanceof GridLayoutManager) {
            final GridLayoutManager gridLayoutManager = (GridLayoutManager) layoutManager;
            this.spanCount = gridLayoutManager.getSpanCount();
        }
        this.horizontalSpace = horizontalSpace;
        this.verticalSpace = verticalSpace;
        this.includeEdge = includeEdge;
    }

    @Override
    public void getItemOffsets(@NonNull Rect outRect, @NonNull View view, @NonNull RecyclerView parent, @NonNull RecyclerView.State state) {
        if (mRecyclerView == null) {
            return;
        }
        int position = parent.getChildAdapterPosition(view);
        if (mRecyclerView.isHeader(position) || mRecyclerView.isFooter(position) || mRecyclerView.isLoadMore(position)) {
            return;
        }
        int column = (position - mRecyclerView.getHeadersCount()) % spanCount;
        if (includeEdge) {
            outRect.left = horizontalSpace - column * horizontalSpace / spanCount;
            outRect.right = (column + 1) * horizontalSpace / spanCount;
            outRect.bottom = verticalSpace;
        } else {
            outRect.left = column * horizontalSpace / spanCount;
            outRect.right = horizontalSpace - (column + 1) * horizontalSpace / spanCount;
            outRect.bottom = verticalSpace;
        }
    }
}